home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / xshp15.zip / OLIST.C < prev    next >
Text File  |  1992-01-02  |  3KB  |  83 lines

  1. /* Object list-related functions. */
  2. #include <stdio.h>
  3. #include "polygon.h"
  4.  
  5. /* Set up the empty object list, with sentinels at both ends to
  6.    terminate searches */
  7. void InitializeObjectList()
  8. {
  9.    ObjectListStart.NextObject = &ObjectListEnd;
  10.    ObjectListStart.PreviousObject = NULL;
  11.    ObjectListStart.CenterInView.Z = INT_TO_FIXED(-32768);
  12.    ObjectListEnd.NextObject = NULL;
  13.    ObjectListEnd.PreviousObject = &ObjectListStart;
  14.    ObjectListEnd.CenterInView.Z = 0x7FFFFFFFL;
  15.    NumObjects = 0;
  16. }
  17.  
  18. /* Adds an object to the object list, sorted by center Z coord. */
  19. void AddObject(Object *ObjectPtr)
  20. {
  21.    Object *ObjectListPtr = ObjectListStart.NextObject;
  22.  
  23.    /* Find the insertion point. Guaranteed to terminate because of
  24.       the end sentinel */
  25.    while (ObjectPtr->CenterInView.Z > ObjectListPtr->CenterInView.Z) {
  26.       ObjectListPtr = ObjectListPtr->NextObject;
  27.    }
  28.  
  29.    /* Link in the new object */
  30.    ObjectListPtr->PreviousObject->NextObject = ObjectPtr;
  31.    ObjectPtr->NextObject = ObjectListPtr;
  32.    ObjectPtr->PreviousObject = ObjectListPtr->PreviousObject;
  33.    ObjectListPtr->PreviousObject = ObjectPtr;
  34.    NumObjects++;
  35. }
  36.  
  37. /* Resorts the objects in order of ascending center Z coordinate in
  38.    view space, by moving each object in turn to the correct position
  39.    in the object list. */
  40. void SortObjects()
  41. {
  42.    int i;
  43.    Object *ObjectPtr, *ObjectCmpPtr, *NextObjectPtr;
  44.  
  45.    /* Start checking with the second object */
  46.    ObjectCmpPtr = ObjectListStart.NextObject;
  47.    ObjectPtr = ObjectCmpPtr->NextObject;
  48.    for (i=1; i<NumObjects; i++) {
  49.       /* See if we need to move backward through the list */
  50.       if (ObjectPtr->CenterInView.Z < ObjectCmpPtr->CenterInView.Z) {
  51.          /* Remember where to resume sorting with the next object */
  52.          NextObjectPtr = ObjectPtr->NextObject;
  53.          /* Yes, move backward until we find the proper insertion
  54.             point. Termination guaranteed because of start sentinel */
  55.          do {
  56.             ObjectCmpPtr = ObjectCmpPtr->PreviousObject;
  57.          } while (ObjectPtr->CenterInView.Z <
  58.                ObjectCmpPtr->CenterInView.Z);
  59.  
  60.          /* Now move the object to its new location */
  61.          /* Unlink the object at the old location */
  62.          ObjectPtr->PreviousObject->NextObject =
  63.                ObjectPtr->NextObject;
  64.          ObjectPtr->NextObject->PreviousObject =
  65.                ObjectPtr->PreviousObject;
  66.  
  67.          /* Link in the object at the new location */
  68.          ObjectCmpPtr->NextObject->PreviousObject = ObjectPtr;
  69.          ObjectPtr->PreviousObject = ObjectCmpPtr;
  70.          ObjectPtr->NextObject = ObjectCmpPtr->NextObject;
  71.          ObjectCmpPtr->NextObject = ObjectPtr;
  72.  
  73.          /* Advance to the next object to sort */
  74.          ObjectCmpPtr = NextObjectPtr->PreviousObject;
  75.          ObjectPtr = NextObjectPtr;
  76.       } else {
  77.          /* Advance to the next object to sort */
  78.          ObjectCmpPtr = ObjectPtr;
  79.          ObjectPtr = ObjectPtr->NextObject;
  80.       }
  81.    }
  82. }
  83.